package view
{
	import data.MonsterData;
	
	import events.MonsterDieEvent;
	
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	
	public class Monster extends Sprite
	{
		public function Monster(_xmlList:XMLList)
		{
			xmlList = _xmlList
			
				inits();
		}
		
		private function inits():void
		{
			addMonsterImg();
			hpLineView = new Sprite();
			addChild(hpLineView)
			moctionSpeed = xmlList.@moctionSpeed
			lrNum = xmlList.@lrNum
			upNum = xmlList.@upNum
			downNum = xmlList.@downNum
			attNum = xmlList.@attNum
			dieNum = xmlList.@dieNum
			radiusX = xmlList.@radiusX
			radiusY = xmlList.@radiusY
			maskWidth = xmlList.@maskWidth
			maskHeigth = xmlList.@maskHeigth
			hpLineViewX = xmlList.@hpLineViewX
			hpLineViewY = xmlList.@hpLineViewY
			hpLineHeight = xmlList.@hpLineHeight
		}
		
		private var lrNum:uint = 14;
		private var upNum:uint = 27;
		private var downNum:uint = 40;
		private var attNum:uint = 63;
		private var dieNum:uint = 71;
		private var radiusX:int = -32;
		private var radiusY:int = -40;
		private var maskWidth:uint = 70;
		private var maskHeigth:uint = 65;
		private var hpLineViewX:Number;
		private var hpLineViewY:Number;
		private var hpLineHeight:Number
		
		/**
		 *血条 
		 */		
		private var hpLineView:Sprite;
		/**
		 *血条长度 
		 */		
		private var _hpLineLong:uint;
		/**
		 *怪物移动方向 
		 */		
		private var _direction:uint = 1;
		/**
		 *素材图片 
		 */		
		private var aMonster:Loader;
		/**
		 *遮罩 
		 */		
		private var mask2:Sprite;
		/**
		 *怪物动画timer 
		 */		
		private var timer:Timer;
		/**
		 *图片的位置索引 
		 */		
		private var i:int;
		/**
		 *怪物动画播放速度 
		 */		
		private var moctionSpeed:Number;
		/**
		 *xmlList 
		 */		
		private var xmlList:XMLList;
		/**
		 *加载素材图片并且给遮罩 
		 * 
		 */			
		protected function addMonsterImg():void
		{
			
			aMonster = new Loader();
			addChild(aMonster);
			aMonster.contentLoaderInfo.addEventListener(Event.COMPLETE,imgloaded);
			aMonster.load(new URLRequest(xmlList.@file));
			
			aMonster.mask = drawMask();
			
		}
		/**
		 *返回血条显示长度 
		 * @return 
		 * 
		 */		
		public function get hpLineLong():uint
		{
			return _hpLineLong;
		}
		/**
		 *set血条显示长度并且更新长度 
		 * @param value
		 * 
		 */		
		public function set hpLineLong(value:uint):void
		{
			_hpLineLong = value;
			if(hpLineView)
			{
			updateHp(_hpLineLong);
			}
		}
		/**
		 *返回方向 
		 * @return 
		 * 
		 */		
		public function get direction():uint
		{
			return _direction;
		}
		/**
		 *方向
		 * 0左，1下，2右，3上，4死，5攻击 
		 * @param value
		 * 
		 */		
		public function set direction(value:uint):void
		{
			_direction = value;
		}
		/**
		 *更新血条显示长度 
		 * @param _crtHpLine
		 * 
		 */		
		private function updateHp(_crtHpLine:Number=35):void
		{			
			hpLineView.x = hpLineViewX;
			hpLineView.y = hpLineViewY;
			hpLineView.graphics.clear();
			hpLineView.graphics.beginFill(0x660000);
			hpLineView.graphics.drawRect(0,0,_crtHpLine,hpLineHeight);
			hpLineView.graphics.endFill();
		}
		/**
		 *画遮罩 
		 * @return 
		 * 
		 */		
		private function drawMask():Sprite
		{
			mask2 = new Sprite();
			addChild(mask2);
			var g:Graphics = mask2.graphics;
			g.beginFill(0x0);
			g.drawRect(radiusX,radiusY,maskWidth,maskHeigth);
			g.endFill();
			return mask2;
			
		}
		
		/**
		 *设置中心点-开始动画 
		 * @param event
		 * 
		 */		
		protected function imgloaded(event:Event):void
		{
			
			aMonster.content.x = radiusX;
			aMonster.content.y = radiusY;
			timer = new Timer(moctionSpeed);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER,timerEvent);
		}
		/**
		 *判断要播放那个动作 
		 * @param event
		 * 
		 */		
		protected function timerEvent(event:TimerEvent):void
		{
			
			switch(direction){
				case 0:
					left();
					break;
				
				case 1:
					down();
					break;
				
				case 2:
					right();
					break;
				
				case 3:
					up();
					break;
				
				case 4:
					die();
					break;
				
				case 5:
					attick();
					break;
			}
		}
		/**
		 *死亡动画 
		 * 
		 */		
		public function die():void
		{
			if(i<attNum+1){
				i=attNum+1
			}
			aMonster.scaleX = 1;
			aMonster.x = -xmlList.textureregion[i].@x;
			aMonster.y = -xmlList.textureregion[i].@y;
			i++						
			if(i>=dieNum){
				remove();
			};
		}
		/**
		 *删除 
		 * 
		 */		
		private function remove():void
		{
			removeChild(aMonster);
			aMonster = null;
			removeChild(mask2)
			mask2 = null
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER,timerEvent);
			timer = null;
			xmlList = null
			removeChild(hpLineView)
			hpLineView = null
			dispatchEvent(new Event("died",true));
		}
		/**
		 *攻击动画
		 * 
		 */		
		private function attick():void
		{
			if(i<downNum+1){
				i=downNum+1
			}
			aMonster.scaleX = 1;
			aMonster.x = -xmlList.textureregion[i].@x;
			aMonster.y = -xmlList.textureregion[i].@y;
			i++						
			if(i>=attNum){
				i=downNum+1
			};
		}
		/**
		 *向左移动动画
		 * 
		 */		
		private function left():void
		{
			if(i>lrNum-1){
				i=0
			};
			aMonster.scaleX = -1;
			aMonster.x = xmlList.textureregion[i].@x;
			aMonster.y = -xmlList.textureregion[i].@y;
			i++
			
		}
		/**
		 *向下移动动画 
		 * 
		 */		
		private function down():void
		{
			if(i<upNum){
				i=upNum
			}
			aMonster.scaleX = 1;
			aMonster.x = -xmlList.textureregion[i].@x;
			aMonster.y = -xmlList.textureregion[i].@y;
			i++						
			if(i>=downNum){
				i=upNum
			};
		}
		/**
		 *向右移动动画 
		 * 
		 */		
		private function right():void
		{
			aMonster.scaleX = 1;
			aMonster.x = -xmlList.textureregion[i].@x;
			aMonster.y = -xmlList.textureregion[i].@y;
			i++							
			if(i>=lrNum){
				i=0
			};
			
		}
		/**
		 *向上移动动画 
		 * 
		 */		
		private function up():void
		{
			if(i<lrNum+1){
				i=lrNum+1
			}
			aMonster.scaleX = 1;
			aMonster.x = -xmlList.textureregion[i].@x;
			aMonster.y = -xmlList.textureregion[i].@y;
			i++						
			if(i>=upNum){
				i=lrNum+1
			};
		}
	}
	
}